package leetcode.editor.cn;

public class P463IslandPerimeter {
    public static void main(String[] args) {
        Solution solution = new P463IslandPerimeter().new Solution();

    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        boolean[][] visited;
        int sum = 0;

        public int islandPerimeter(int[][] grid) {
            int rowLength = grid.length;
            int colLength = grid[0].length;
            int math = 0;
            visited = new boolean[rowLength][colLength];
            for (int i = 0; i < rowLength; i++) {
                for (int j = 0; j < colLength; j++) {
                    if (grid[i][j] == 1) {
                        math += dfs(grid, i, j);
                    }
                }
            }
            return math;
        }

        public int dfs(int[][] grid, int row, int col) {
            int rowLength = grid.length;
            int colLength = grid[0].length;
            if (row >= rowLength || row < 0 || col >= colLength || col < 0
                    || grid[row][col] == 0) {
                return 1;
            }
            if (visited[row][col]) {
                return 0;
            }
            visited[row][col] = true;
            return dfs(grid, row + 1, col) +
                    dfs(grid, row, col + 1) +
                    dfs(grid, row - 1, col) +
                    dfs(grid, row, col - 1);

        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}